#include "paywidget.h"
#include "ui_paywidget.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QMessageBox>
#include <QDateTime>

PayWidget::PayWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PayWidget)
{
    ui->setupUi(this);
    initWindow();
}

PayWidget::~PayWidget()
{
    delete ui;
}

/**
 * @brief 初始化窗体
 */
void PayWidget::initWindow()
{
    this->setWindowTitle("缴费");
    QSqlQuery query;

    query.prepare("SELECT id, username FROM user WHERE role=3"); //查询全部业主
    if (!query.exec())
    {
        qDebug() << query.lastError();
    }
    else
    {
        while(query.next())
        {
            int id = query.value(0).toInt();
            QString username = query.value(1).toString();
            ui->cb_user->insertItem(id, username);
        }
    }
    query.prepare("SELECT type FROM price_manage");
    if (!query.exec())
    {
        qDebug() << query.lastError();
    }
    else
    {
        while(query.next())
        {
            QString type = query.value(0).toString();
            ui->cb_type->addItem(type);
        }
    }
}

void PayWidget::initTable()
{
    QString username = ui->cb_user->currentText();
    QString type = ui->cb_type->currentText();

    model = new QSqlTableModel(this);
    model->setTable("pay_record"); //指定表
    model->setFilter(QString("pay_username='%1' AND pay_type = '%2'").arg(username).arg(type));
    model->setHeaderData(1,Qt::Horizontal,"缴费用户");
    model->setHeaderData(2,Qt::Horizontal,"缴费类型");
    model->setHeaderData(3,Qt::Horizontal,"是否缴费");
    model->setHeaderData(4,Qt::Horizontal, "缴费日期");
    model->select(); //查询
    model->setEditStrategy(QSqlTableModel::OnManualSubmit); //Model编辑策略，提交生效
    ui->tableView->setModel(model); //将模型设置到视图中
    ui->tableView->setColumnHidden(0, 1); //隐藏编号列
    ui->tableView->setColumnHidden(0, 5); //隐藏record
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);   //使其不可编辑
}

/**
 * @brief 缴费
 */
void PayWidget::on_btn_pay_clicked()
{
    int curRow = ui->tableView->currentIndex().row(); //获取选中行号

    if(curRow < 0)
    {
        QMessageBox::information(this, tr("提示"), tr("请先选中要缴费的记录"), QMessageBox::Ok);
        return;
    }

    QString isPay = model->itemData(model->index(curRow, 3))[0].toString();
    if(isPay == "已缴费")
    {
        QMessageBox::information(this, tr("提示"), tr("该记录已缴费，不能再次缴费"), QMessageBox::Ok);
        return;
    }

    int id = model->itemData(model->index(curRow, 0))[0].toInt();
    QString username = model->itemData(model->index(curRow, 1))[0].toString();
    QString type = model->itemData(model->index(curRow, 2))[0].toString();

    //更新选中记录的缴费情况为已缴费
    QSqlQuery query;
    query.prepare("UPDATE pay_record SET is_pay='已缴费', pay_datetime=:pay_datetime WHERE id=:id");
    query.bindValue(":id", id);
    query.bindValue(":pay_datetime", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    if (!query.exec())
    {
        qDebug() << query.lastError();
    }
    else
    {
        QMessageBox::information(this, tr("提示"), tr("缴费成功"), QMessageBox::Ok);
    }

    //获取刚才更新记录的record_id
    query.prepare("SELECT record_id FROM pay_record WHERE id=:id");
    query.bindValue(":id", id);
    query.exec();
    query.next();
    int record_id = query.value(0).toInt();
    qDebug() << record_id << type;

    //根据record_id更新record对应的缴费情况
    if (type == "维修费")
    {
        query.prepare("UPDATE repair_apply SET is_pay = '已缴费' WHERE id=:id");
        query.bindValue(":id", record_id);
        query.exec();
    }
    else if(type == "车位费")
    {
        query.prepare("UPDATE parking_apply SET is_pay = '已缴费' WHERE id=:id");
        query.bindValue(":id", record_id);
        query.exec();
    }
    initTable();
}


void PayWidget::on_cb_type_currentIndexChanged(int index)
{
    initTable();

}

void PayWidget::on_cb_user_currentIndexChanged(int index)
{
    initTable();
}
